//
//  NewFriendListVC.m
//  ChangChat
//
//  Created by Cocoa on 2019/9/3.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "NewFriendListVC.h"
#import "YBApiManager+Friend.h"
#import "FriendCell.h"
#import "NewFriendCell.h"
#import "FriendInfoListVC.h"
#import "IMManager.h"

@interface NewFriendListVC ()<IMManagerDelegate>

@end

@implementation NewFriendListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pContentView.type = 666;
    [self.tableView customRegisterClassWithName:@"NewFriendCell"];
    [self requestDatas];
    self.title = @"新朋友";
    [IMManager.shareManager addIMDelegate:self];
}

- (void)requestDatas {
    if (self.dataSource.count <= 0) {    
        [MBProgressHUD showTipMessageInView:@""];
    }
    @weakify(self);
    [[YBApiManager.shareManager getNewFriendList] subscribeNext:^(id  _Nullable x) {
        [MBProgressHUD hideHUD];
        @strongify(self);
        NSArray *dataArr = [x objectForKey:@"data"];
        self.dataSource = [FriendItem mj_objectArrayWithKeyValuesArray:dataArr];
        [self.tableView reloadData];
    } error:^(NSError * _Nullable error) {
        [self.dataSource removeAllObjects];
        [self.tableView reloadData];
        [MBProgressHUD hideHUD];
        if ([error isServerError]) {
            [MBProgressHUD showTipMessageInView:error.localizedDescription timer:1.0f];
        } else {
            [MBProgressHUD showTipMessageInView:@"网络错误，请重试" timer:1.0f];
        }
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NewFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewFriendCell" forIndexPath:indexPath];
    @weakify(self);
    cell.friendItem = self.dataSource[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [[[cell.agreeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:cell.rac_prepareForReuseSignal] subscribeNext:^(__kindof UIControl * _Nullable x) {
        FriendInfoListVC *friendVc = [FriendInfoListVC new];
        friendVc.user = self.dataSource[indexPath.row];
        friendVc.from = friendVc.user.type;
        friendVc.listType = 1;
        @strongify(self);
        [friendVc setAgreenApplyBlock:^{
            [self requestDatas];
        }];
        [self.navigationController pushViewController:friendVc animated:YES];
    }];
    [[[cell.rejectBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:cell.rac_prepareForReuseSignal] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [LBXAlertAction showAlertWithTitle:@"提示" msg:@"确认拒绝？" buttonsStatement:@[@"取消", @"确认"] chooseBlock:^(NSInteger buttonIdx) {
            if (buttonIdx == 1) {            
                @strongify(self);
                [self rejectAddFriendActionWithIndexPath:indexPath];
            }
        }];
    }];
    return cell;
}
- (void)rejectAddFriendActionWithIndexPath:(NSIndexPath *)indexPath {
    FriendItem *friendItem = self.dataSource[indexPath.row];
    [MBProgressHUD showActivityMessageInView:@""];
    @weakify(self);
    [[YBApiManager.shareManager rejectFriendRequestWithFriend_id:friendItem.friend_id] subscribeNext:^(id  _Nullable x) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showTipMessageInView:@"操作成功" timer:1.0f];
        @strongify(self);
        [self requestDatas];
    } error:^(NSError * _Nullable error) {
        [MBProgressHUD hideHUD];
        if ([error isServerError]) {
            [MBProgressHUD showTipMessageInView:error.localizedDescription timer:1.0f];
        } else {
            [MBProgressHUD showTipMessageInView:@"网络错误，请重试" timer:1.0f];
        }
    }];
}
#pragma mark immanager delegate
- (void)imManager:(IMManager *)manager receiveFriendStatusMsg:(MsgItem *)msg {
    if (msg.type == MsgType_Friend_apply) {
        [self requestDatas];
    } else if (msg.type == MsgType_Friend_agree) {
        [self requestDatas];
    } else if (msg.type == MsgType_Friend_rejected) {
        [self requestDatas];
    }
}
@end
